package cn.tuling.example;

/**
 * 例子：用责任链模式设计一个请假条审批模块
 * 分析：假如规定学生请假小于或等于 2 天，班主任可以批准；小于或等于 7 天，系主任可以批准；小于或等于 10 天，院长可以批准；其他情况不予批准；
 */
public class ChainOfResponsibilityTest {
    public static void main(String[] args) {
        Leader classAdviser = new ClassAdviser();
        Leader departmentHead = new DepartmentHead();
        Leader dean = new Dean();

        classAdviser.setNext(departmentHead);
        departmentHead.setNext(dean);

        classAdviser.handle(2);
        classAdviser.handle(5);
        classAdviser.handle(8);
        classAdviser.handle(11);
    }
}

abstract class Leader {
    private Leader next;

    public Leader getNext() {
        return next;
    }

    public void setNext(Leader next) {
        this.next = next;
    }

    void handle(int leaveDays) {
        if (doHandle(leaveDays))
            return;

        if (getNext() != null)
            getNext().handle(leaveDays);
        else
            System.out.println("不予批准");
    }

    protected abstract boolean doHandle(int leaveDays);
}

class ClassAdviser extends Leader {

    @Override
    protected boolean doHandle(int leaveDays) {
        if (leaveDays <= 2) {
            System.out.println("班主任批准假期");
            return true;
        }
        return false;
    }
}

class DepartmentHead extends Leader {

    @Override
    protected boolean doHandle(int leaveDays) {
        if (leaveDays <= 7) {
            System.out.println("系主任批准假期");
            return true;
        }
        return false;
    }
}

class Dean extends Leader {

    @Override
    protected boolean doHandle(int leaveDays) {
        if (leaveDays <= 10) {
            System.out.println("院长批准假期");
            return true;
        }
        return false;
    }
}
